#include "BDS.hpp"
String format2char(uint8_t data)
{
  char chs[3];
  int width = 2;
  sprintf(chs,"%0*d",width,data);
  return String(chs);
}
void BDS::init_BDS(BDS::_BaudRate baudRate , int tx_pin, int rx_pin)
{
  Serial2.begin(baudRate, SERIAL_8N1, rx_pin, tx_pin);//初始化串口1
  if (!Serial2)
  {
    Serial.println("BDS uart init Error!");
  }
  else{
    Serial.println("BDS uart init OK!");
  }
  //先填入空白数据
  fixed_point.time_str = format2char((gps.time.hour() + 8 >= 24) ? (gps.time.hour()-16) : gps.time.hour() + 8) + String(F(":")) + format2char(gps.time.minute()) + String(F(":")) + format2char(gps.time.second());
}


bool BDS::check_Data(void)
{
//     while(serial_connection.available())//While there are characters to come from the GPS
//   {
//     gps.encode(serial_connection.read());//This feeds the serial NMEA data into the library one char at a time
//   }
  if (Serial2.available()) // 说明正在传输数据
  {
    if (!gps.encode(Serial2.read()))//This feeds the serial NMEA data into the library one char at a time
    return false;
  }
  
  if(gps.location.isUpdated())//This will pretty much be fired all the time anyway but will at least reduce it to only after a package of NMEA data comes in
  {
    //Get the latest info from the gps object which it derived from the data sent by the GPS unit
    fixed_point.latitude = gps.location.lat();
    fixed_point.longitude = gps.location.lng();
    fixed_point.time_str = format2char((gps.time.hour() + 8 >= 24) ? (gps.time.hour()-16) : gps.time.hour() + 8) + String(F(":")) + format2char(gps.time.minute()) + String(F(":")) + format2char(gps.time.second());

    // Serial.print("The BDS recved longitude = ");
    // Serial.print(fixed_point.longitude, 6);
    // Serial.print(" , latitude = ");
    // Serial.print(fixed_point.latitude, 6);
    // Serial.print(" , time = ");
    // Serial.println(fixed_point.time_str);
    // %.6f , latitude = %.6f .",fixed_point.longitude, fixed_point.latitude);
    // Serial.println(fixed_point.longitude, 6);
    return true;
  }
  else
  {
    // Serial.println("Cann't format the msg, please check data if the result occured repeatly!");
  }
  return false;
}